package activity

import (
	"fmt"
	"github.com/zserge/lorca"
	"mylorca/control"
	"reflect"
	"strings"
)

func bindClick(c interface{}) {
	tType := reflect.TypeOf(c)
	vType := reflect.ValueOf(c)
	for i := 0; i < tType.NumMethod(); i++ {
		name := tType.Method(i).Name
		clickName := strings.ToLower(name[:1]) + name[1:]
		m := vType.MethodByName(name)
		control.UI.Bind(clickName, func() {
			m.Call(nil)
		})
	}
}

func findByValue(id string) *View {
	return &View{id: id}
}

type View struct {
	id    string
	value lorca.Value
}

func (v *View) set() {
	control.UI.Eval(fmt.Sprintf("window.vue.%s = '%s'", v.id, v.value))
}

func (v *View) get() lorca.Value {
	v.value = control.UI.Eval(fmt.Sprintf("window.vue.%s", v.id))
	return v.value
}
